﻿#include "mymap.h"
#include "vertexitem.h"
#include <QUrl>

MyMap::MyMap(QObject *parent) :
    QGraphicsScene(parent)
{
  isStartLine = false;
  firstVertex = NULL;
  secondVertex = NULL;
  startLine = NULL;
  endLine=NULL;
  pen = new QPen(Qt::black, 1, Qt::SolidLine);
  vertexes.clear();
  orient = false;
}

void MyMap::dropEvent(QGraphicsSceneDragDropEvent *event){


}

void MyMap::dragEnterEvent(QGraphicsSceneDragDropEvent *event){

    event->accept();
}

void MyMap::dragMoveEvent(QGraphicsSceneDragDropEvent *event){

    event->accept();
}

void MyMap::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
event->accept();
}

void MyMap::mousePressEvent(QGraphicsSceneMouseEvent *event){


event->accept();
}

void MyMap::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
event->accept();
}

void MyMap::startVertex(QPoint *p){
    //qDebug()<<p->x()<<p->y()<<"--";

    if ( this->items( p->x()-20,p->y()-20,40,40 ).size()>0 ){
        return;
    }

    this->addVertex( p->x(),p->y()  );
}

void MyMap::startstopLine(QPoint *p){

    QList<QGraphicsItem *> items;
    items = this->items( p->x()-10,p->y()-10,20,20 );

    VertexItem *item;
    item = NULL;

    for ( int i=0; i<items.size(); i++ ){
        item = dynamic_cast<VertexItem *>( items.at(i) );
        if ( item ){
            break;
        }else{
            item = NULL;
        }
    }

    //VertexItem *item = dynamic_cast<VertexItem *>( this->items( p->x()-10,p->y()-10,20,20 ).first() );

    //qDebug()<<item->metaObject()->className();
    if ( item ){
        if ( isStartLine ){ //если линию заканчиваем(она уже начата)
            secondVertex = item;

            //проверяем - если точки не нулевые и не совпадают и нет связи уже
            if ( secondVertex!=NULL &&
                 firstVertex!=NULL &&
                 secondVertex->getIndex()!=firstVertex->getIndex() &&
                 !firstVertex->isLinkWith( secondVertex->getIndex() ) ){


               firstVertex->addLink( secondVertex->getIndex() );
               if ( !orient ){
                  secondVertex->addLink( firstVertex->getIndex() );
                  pen->setColor(Qt::black);
               }else{
                  pen->setColor(Qt::red);
               }


               endLine = new QPoint();
               endLine->setX( item->pos().x() );
               endLine->setY( item->pos().y() );

               float sinA,cosA,l,diam,x,y,sx,sy,ex,ey;
               diam = 10;
               x = endLine->x()-startLine->x();
               y = endLine->y()-startLine->y();
               l = qSqrt( x*x+y*y );

               sinA = y/l;
               cosA = x/l;

               sx = startLine->x() + diam*cosA;
               sy = startLine->y() + diam*sinA;

               ex = endLine->x() - diam*cosA;
               ey = endLine->y() - diam*sinA;

               this->addLine( sx,sy,ex,ey, *pen );

               //рисуем стрелку
               if ( orient ){
                    float A,B,W,Lmin,PI,px1,px2,py1,py2,Ysign;
                    PI = 3.14159265;
                    Lmin = 5;
                    B = PI/6; //30 deg
                    A = qAtan( x/y );

                    if ( y<0 ) Ysign=-1;
                    else Ysign = 1;

                    W = Ysign*PI/2-(A+B);

                    px1 = ex - Lmin*qCos( W );
                    py1 = ey - Lmin*qSin( W );

                    W = W + 2*B;
                    px2 = ex - Lmin*qCos( W );
                    py2 = ey - Lmin*qSin( W );
                    this->addLine( px1,py1,ex,ey, *pen );
                    this->addLine( px2,py2,ex,ey, *pen );
               }



               emit calculateMatrix();
            }

            isStartLine = false;
            endLine = NULL;
            startLine = NULL;

        }else{ //если начинаем линию только вести
            firstVertex = item;
            secondVertex = NULL;

            startLine = new QPoint();
            startLine->setX( item->pos().x() );
            startLine->setY( item->pos().y() );
            endLine = NULL;
            isStartLine = true;
        }
    }




}

//смена ориентации
void MyMap::changeOrient(bool *o){
    orient = *o;
    return;
}


//находим элемент под указателем
bool MyMap::findMyItem( QPoint p){
    if ( this->items( p.x()-10,p.y()-10,20,20 ).size()>0 ){
       //return qobject_cast<VertexItem *> (this->items( p.x()-10,p.y()-10,20,20 ))  ;
    }
    return false;
}

void MyMap::addVertex(int x, int y){
   int index;
   index = vertexes.size()+1;
   //qDebug()<<index;

   VertexItem *item = new VertexItem();
   item->setXX(x);
   item->setYY(y);
   item->setIndex(index);

   this->addItem( item );

   vertexes.resize( vertexes.size() );
   vertexes.append( index );

}
